export function luhnCheck(cardNumber) {
	const cleaned = cardNumber.replace(/\D/g, '');
	if (!/^\d{12,19}$/.test(cleaned)) {
		return false;
	}

	let sum = 0;
	for (let i = 0; i < cleaned.length; i++) {
		let digit = parseInt(cleaned[cleaned.length - 1 - i], 10);
		if (i % 2 === 1) {
			digit *= 2;
			if (digit > 9) {
				digit = digit - 9;
			}
		}
		sum += digit;
	}

	return sum % 10 === 0;
}

export function validateBankCard(cardNumber) {
	const bankCardRegex = /^[0-9]{12,19}$/;
	if (!bankCardRegex.test(cardNumber)) {
		return false;
	}
	return luhnCheck(cardNumber);
}

export function getCardType(cardNumber) {
	if (/^62/.test(cardNumber)) {
		return '中国银联';
	} else if (/^4/.test(cardNumber)) {
		return 'Visa';
	} else if (/^5[1-5]/.test(cardNumber) || /^222[1-9]/.test(cardNumber) || /^22[3-9]/.test(cardNumber) || /^2[3-6]/
		.test(cardNumber) || /^27[0-1]/.test(cardNumber) || /^2720/.test(cardNumber)) {
		return 'MasterCard';
	} else if (/^3[47]/.test(cardNumber)) {
		return 'American Express';
	} else {
		return '未知';
	}
}

// 示例用法
// const cardNumber = '6228480402564890018'; // 示例银行卡号
// console.log('卡号是否有效:', validateBankCard(cardNumber)); // 输出: true 或 false
// console.log('卡类型:', getCardType(cardNumber)); // 输出: 中国银联


// 正整数校验
export const zzshuRegFn = (inputValue) => {
	const regex = /^\d+$/; // 正整数正则表达式
	if (!regex.test(inputValue)) {
		return false
	} else {
		return true
	}
};